package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.dto.MatchDto;
import com.example.speeddating.entity.Match;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.speeddating.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-07-21 08:00:57
 */
@Mapper
public interface MatchMapper extends BaseMapper<Match> {


    @Update("update `match` set is_read = 1 where match_id = #{userId} and is_read = 0")
    public void updateMatchUser(long userId);

    @Select("select u.* from `match` m JOIN `user` u ON u.id = m.user_id WHERE m.match_id = #{userId}")
    public Page<User> selectMatchUser(Page<User> userPage,long userId);

    @Select("select count(*) from `match` m JOIN `user` u ON u.id = m.user_id WHERE m.match_id = #{userId} AND m.is_read = 0")
    public Integer selectMatchUserCount(long userId);

    @Select("SELECT COUNT(*) AS total FROM `match` WHERE user_id = #{userId} AND deleted=0 AND (DATE(create_time) = CURDATE())")
    Integer selectCountMatchNum(Long userId);


    @Select("SELECT COUNT(*) AS total FROM `match` WHERE deleted=0 AND (DATE(create_time) = CURDATE())")
    Integer selectCountMatchNum();

    @Select("SELECT match_id FROM `match` WHERE user_id = #{userId}")
    List<Long> selectMatchedUserId(Long userId);

    @Insert("insert into `match` values(0,#{userId},#{matchId},0,NOW(),0,1)")
    void insertMatch(Match match);

    @Select("SELECT COUNT(*) AS total FROM `match` WHERE deleted=0 AND (DATE(create_time) = CURDATE()) AND user_id = #{id}")
    Integer selectLeaveCount(Long id);

    @Select("SELECT COUNT(*) AS total FROM `match` WHERE deleted=0")
    Integer selectCountTotalMatchNum();

    @Select("SELECT DATE(create_time) AS date, COUNT(*) AS match_count\n" +
            "FROM `match`\n" +
            "WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)\n" +
            "GROUP BY DATE(create_time)\n" +
            "ORDER BY DATE(create_time) ASC;")
    ArrayList<MatchDto> selectWeekMatch();
}
